package com.example.validatestudy.demos.dataStructures;

import java.util.Arrays;

/**
 * @param
 * @version v1
 * @auther WANGZHENFANG004
 * @summary 数组练习
 * @since 2024 06 22 15 52
 */
public class ArrayTest {

    public static void main(String[] args) {
        //静态初始化
        int[] array1 = {1,2,3,4,5};
        int[] array2 = new int[]{1,2,3,4,5};
        int[][] arr3 = {{1,2},{3,4}};
        int[][] arr4 = new int[][]{{1,2},{3,4}};

        //动态初始化
        int[] a = new int[5];
        int[][] b = new int[2][3]; //2行3列
        int[][] c = new int[3][]; //可以省略列

        /**
         * 遍历
         * 二维数组中元素的个数是行数，即 array.length，列数为每个元素中的元素个数，即 array[i].length
         */
        arrPrint();

        /**
         * Arrays方法练习
         */
        arraysTest();

        /**
         * 做题
         */
        doWork();
    }


    public static void arrPrint(){
        int[] arr1 = {1,2,3,4,5};
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }

        int[][] arr2 = {{1,2,3},{4,5,6}};
        for (int i = 0; i < arr2.length; i++) {
            for (int j = 0; j < arr2[i].length; j++) {
                System.out.println(arr2[i][j]);
            }
        }
    }

    public static void arraysTest(){
        //打印
        int[] arr = {3,2,1,4,5};
        System.out.println(Arrays.toString(arr));

        //排序
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        //拷贝到新的数组
        int[] arr2 = Arrays.copyOf(arr,10);
        System.out.println(Arrays.toString(arr2));
        //注意是左闭右开 [0，6）
        int[] arr3 = Arrays.copyOfRange(arr2,0,6);
        System.out.println(Arrays.toString(arr3));
        int[] arr4 = new int[4];
        //System.arraycopy 最灵活，拷贝指定元素到指定数组
        System.arraycopy(arr2,0,arr4,1,3);
        System.out.println(Arrays.toString(arr4));

        int[][] arr5 = {{1,2,3},{4,5,6}};
        System.out.println(Arrays.toString(arr5));
        System.out.println(Arrays.deepToString(arr5));

    }

   
    public static void doWork(){

        int[] arr = {1,0,3,4};
        int[] arr1 = {1,0,3,0,5};
        int[] arr2 = {0};
        move(arr);
        move(arr1);
        move(arr2);

    }

    /**
     * 给定一个数组 nums，编写一个函数将所有 0 移动到数组的末尾，同时保持非零元素的相对顺序。
     * 请注意 ，必须在不复制数组的情况下原地对数组进行操作。
     *
     * 示例 1:
     * 输入: nums = [0,1,0,3,12]
     * 输出: [1,3,12,0,0]
     * 示例 2:
     * 输入: nums = [0]
     * 输出: [0]
     */
    public static void move(int[] nums){
        //未排序的数组总长度的最大坐标
        int length = nums.length-1;
        for (int i = 0; i < length; i++) {
            if (nums[i] == 0 && i < length) {
                for (int j = i; j < length; j++) {
                    if(nums[j+1]!=0){
                        nums[j] = nums[j+1];
                        nums[j+1] = 0;
                    }
                }
                i = i - 1;
                length = length-1;
            }
        }
        System.out.println(Arrays.toString(nums));
    }
    
}
